﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.Data;

namespace TranTekBE.KPI_NhanSu
{
    public partial class Xem_Yeu_Cau_Chuyen_Ca_Cua_Nhan_Vien : PageRequireLogin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (UserInfo.MemberKPI != true)
            {
                //Load JS
                ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);

                if (Request.QueryString["user"] == null && Request.QueryString["store"] == null && Request.QueryString["time"] == null && Request.QueryString["status"] == null)
                {
                    LoadData("", UserInfo.Stores, "", "Chưa xử lý");
                }
                else
                {
                    LoadData(Request.QueryString["user"], Request.QueryString["store"], Request.QueryString["time"], Request.QueryString["status"]);
                }
                //
                if (!IsPostBack)
                {
                    // Get user
                    FunctionsUtils.GetDropListUser(droplist_ten_nhan_vien_loc_kq);
                    // Get stores
                    FunctionsUtils.GetDropListStore(droplist_cua_hang_loc_kq);
                    droplist_ten_nhan_vien_loc_kq.SelectedValue = Request.QueryString["user"];
                    droplist_cua_hang_loc_kq.SelectedValue = Request.QueryString["store"];
                    txt_thoi_gian_loc.Text = Request.QueryString["time"];
                    droplist_status.SelectedValue = Request.QueryString["status"];

                }
            }
            else
            {
                Response.Redirect("../Admin/DontPermission.aspx");
            }
        }

        // Get Avatar
        public string GetImage(object userId)
        {
            string Avatar = "";
            string fileName = userId + ".png";
            string path_image = Server.MapPath("~/images/KPI/UserImage/") + fileName;
            //
            if (System.IO.File.Exists(path_image))
            {
                Avatar = "../images/KPI/UserImage/" + fileName;
            }
            else
            {
                Avatar = "../images/KPI/UserImage/avatar_default.png";
            }
            return Avatar;
        }

        void LoadData(string getuser, string getstore, string gettime, string getstatus)
        {
            string sql_loc_user = @"SELECT * 
            FROM tbl_YeuCauChuyenCa as ChuyenCa
            Left Join 
            tbl_UserKPI as KPI
            on ChuyenCa.NAME_USER = KPI.NAME_USER
            WHERE 1=1";
            string sql_user = "";
            string sql_stores = "";
            string sql_date = "";
            string sql_status = "";
            if (getuser != "" && getuser != null)
            {
                sql_user = " AND KPI.ID_USER = '" + getuser + "'";
            }

            if (getstore != "" && getstore != null)
            {
                sql_stores = " AND CUA_HANG_DANG_LAM = '" + getstore + "'";
            }
            if (gettime != "" && gettime != null)
            {
                sql_date = " AND NGAY_DANG_LAM = '" + gettime + "'";
            }
            if (getstatus != "" && getstatus != null)
            {
                sql_status = " AND TRANG_THAI = N'" + getstatus + "'";
            }

            sql_loc_user = sql_loc_user + sql_user + sql_stores + sql_date + sql_status;

            DataSet all_user_yeu_cau = FunctionsUtils.Database2.ExecuteQuery(sql_loc_user);

            DataTable dt = all_user_yeu_cau.Tables[0];

            PagedDataSource pgitems = new PagedDataSource();

            System.Data.DataView dv = new System.Data.DataView(dt);

            pgitems.DataSource = dv;

            pgitems.AllowPaging = true;

            pgitems.PageSize = 10;

            pgitems.CurrentPageIndex = PageNumber;

            if (pgitems.PageCount > 1)
            {

                rptPages.Visible = true;

                System.Collections.ArrayList pages = new System.Collections.ArrayList();

                for (int i = 0; i < pgitems.PageCount; i++)

                    pages.Add((i + 1).ToString());

                rptPages.DataSource = pages;

                rptPages.DataBind();

            }

            else

                rptPages.Visible = false;

            rpXemYeuCau.DataSource = pgitems;

            rpXemYeuCau.DataBind();

        }

        public int PageNumber
        {

            get
            {

                if (ViewState["PageNumber"] != null)

                    return Convert.ToInt32(ViewState["PageNumber"]);

                else

                    return 0;

            }

            set
            {

                ViewState["PageNumber"] = value;

            }

        }

        protected void rptPages_ItemCommand1(object source, RepeaterCommandEventArgs e)
        {

            PageNumber = Convert.ToInt32(e.CommandArgument) - 1;

            if (Request.QueryString["user"] == null && Request.QueryString["store"] == null && Request.QueryString["time"] == null && Request.QueryString["status"] == null)
            {
                LoadData("", "", "", "");
            }
            else
            {
                LoadData(Request.QueryString["user"], Request.QueryString["store"], Request.QueryString["time"], Request.QueryString["status"]);
            }

        }
        

        protected void btnLoc_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["user"] == null && Request.QueryString["store"] == null && Request.QueryString["time"] == null && Request.QueryString["status"] == null)
            {
                LoadData("", "", "", "");
            }
            else
            {
                LoadData(Request.QueryString["user"], Request.QueryString["store"], Request.QueryString["time"], Request.QueryString["status"]);
            }

        }

        protected void droplist_ten_nhan_vien_loc_kq_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("Xem-Yeu-Cau-Chuyen-Ca-Cua-Nhan-Vien.aspx?store=" + droplist_cua_hang_loc_kq.SelectedValue.ToString() + "&time=" + txt_thoi_gian_loc.Text + "&user=" + droplist_ten_nhan_vien_loc_kq.SelectedValue.ToString() + "&status=" + droplist_status.SelectedValue.ToString());
        }

        protected void droplist_cua_hang_loc_kq_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("Xem-Yeu-Cau-Chuyen-Ca-Cua-Nhan-Vien.aspx?store=" + droplist_cua_hang_loc_kq.SelectedValue.ToString() + "&time=" + txt_thoi_gian_loc.Text + "&user=" + droplist_ten_nhan_vien_loc_kq.SelectedValue.ToString() + "&status=" + droplist_status.SelectedValue.ToString());
        }

        protected void txt_thoi_gian_loc_TextChanged(object sender, EventArgs e)
        {
            Response.Redirect("Xem-Yeu-Cau-Chuyen-Ca-Cua-Nhan-Vien.aspx?store=" + droplist_cua_hang_loc_kq.SelectedValue.ToString() + "&time=" + txt_thoi_gian_loc.Text + "&user=" + droplist_ten_nhan_vien_loc_kq.SelectedValue.ToString() + "&status=" + droplist_status.SelectedValue.ToString());
        }

        protected void droplist_status_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("Xem-Yeu-Cau-Chuyen-Ca-Cua-Nhan-Vien.aspx?store=" + droplist_cua_hang_loc_kq.SelectedValue.ToString() + "&time=" + txt_thoi_gian_loc.Text + "&user=" + droplist_ten_nhan_vien_loc_kq.SelectedValue.ToString() + "&status=" + droplist_status.SelectedValue.ToString());
        }

        protected void btnAll_Click(object sender, EventArgs e)
        {
            LoadData("", "", "", "");
        }


        //========= Delete buttons inside repeater.
        //protected void rpXemYeuCau_ItemCommand(object source, RepeaterCommandEventArgs e)
        //{
        //    switch (e.CommandName)
        //    {
        //        case ("Delete"):
        //            int id = Convert.ToInt32(e.CommandArgument);
        //            deleteEvent(id);
        //            break;
        //    }
        //    Response.Redirect("Xem-Yeu-Cau-Chuyen-Ca-Cua-Nhan-Vien.aspx");
        //}

        ////===== Method to delete employee from database.
        //void deleteEvent(int id)
        //{
        //    //======= Delete Query.
        //    string cmdText_del = "DELETE FROM tbl_YeuCauChuyenCa WHERE ID_GUI_YEU_CAU_CHUYEN_CA=" + id;
        //    FunctionsUtils.Database2.ExecuteNonQuery(cmdText_del);

        //}
    }
}